package com.base.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.base.comm.CommContent;
import com.base.po.param.ApiState;
import com.base.po.sqlparam.SmsinfoesExamplePage;
import com.base.po.voip.Smsinfoes;
import com.base.po.voip.SmsinfoesExample.Criteria;
import com.base.service.LogsService;
import com.base.service.SmsinfoesService;
import com.base.service.UsersService;
import com.base.utils.GsonUtil;
import com.base.utils.IP;

/**
 * admin管理系统控制器
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin")
public class content_yanzhengmabaobiao {
	@Autowired
	private LogsService logsService;// log日志
	@Autowired
	private SmsinfoesService registerinfoesService;
	@Autowired
	private UsersService usersService;

	// 验证码报表
	@RequestMapping("/content_yanzhengmabaobiao")
	public ModelAndView index(HttpServletRequest request) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		SmsinfoesExamplePage examplePage = new SmsinfoesExamplePage();
		examplePage.pagesize = 10;
		examplePage.start = 0;
		examplePage.rows = examplePage.pagesize;// 设置首次分页
		examplePage.setOrderByClause("CreateTime");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		List<Smsinfoes> smsinfoes = null;
		Criteria criteria = examplePage.createCriteria();
		switch (adminUsergrade) {
		case 0:

			break;
		default:
			examplePage.getOredCriteria().add(criteria.andAgentidEqualTo(adminUserId));
			break;
		}/*
			 * 设置分页
			 */
		int totlenum = registerinfoesService.countByExample(examplePage);// 页面总数
		request.setAttribute("totlenum", totlenum);// 传入session中
		request.setAttribute("curPage", 1);// 传入session中
		smsinfoes = registerinfoesService.selectByExamplePage(examplePage);
		if (smsinfoes != null) {
			for (Smsinfoes s : smsinfoes) {
				String username = usersService.selectByPrimaryKey(s.getAgentid()).getUsername();
				s.setAgentid(username);
			}
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_yanzhengmabaobiao");
			for (Smsinfoes s : smsinfoes) {
				if (s.getApistatus() != null) {
					ApiState newstate = GsonUtil.getObject(s.getApistatus(), ApiState.class);
					s.setApistatus(newstate.getDesc());
				} else {
					s.setApistatus("请求失败");
				}
			}
			request.setAttribute("list", smsinfoes);
			return modelAndView;
		}
		String usernamesys = (String) request.getSession().getAttribute("useracc");
		logsService.insert(CommContent.TYPE_SYS, CommContent.LOGS_READ + "验证码信息", usernamesys, true,
				IP.getIpAddr(request));
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}

	// 验证码报表
	@RequestMapping("/selectSmsinfoes")
	public ModelAndView selectSmsinfoes(HttpServletRequest request, String id, Integer curPage, String mobile,
			String agentid) throws Exception {
		String username = agentid;
		// 设置session缓存
		if (curPage == null) {
			if (request.getSession().getAttribute("curPage") != null) {
				curPage = (Integer) request.getSession().getAttribute("curPage");
			} else {
				curPage = 1;
			}
		}
		if (id == null) {
			if (request.getSession().getAttribute("id") != null) {
				id = (String) request.getSession().getAttribute("id");
			}
		}
		if (mobile == null) {
			if (request.getSession().getAttribute("mobile") != null) {
				mobile = (String) request.getSession().getAttribute("mobile");
			}
		}
		if (username == null) {
			if (request.getSession().getAttribute("username") != null) {
				mobile = (String) request.getSession().getAttribute("username");
			}
		}
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		SmsinfoesExamplePage examplePage = new SmsinfoesExamplePage();
		examplePage.pagesize = 10;
		examplePage.start = (curPage - 1) * examplePage.pagesize;
		examplePage.rows = examplePage.pagesize;// 设置首次分页
		examplePage.setOrderByClause("CreateTime");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		List<Smsinfoes> smsinfoes = null;
		Criteria criteria = examplePage.createCriteria();
		// 条件筛选
		if (id != null && !id.equals("")) {
			request.setAttribute("id", id);
			criteria = criteria.andIdEqualTo(id);
			examplePage.getOredCriteria().add(criteria);
		}
		if (mobile != null && !mobile.equals("")) {
			request.setAttribute("mobile", mobile);
			criteria = criteria.andMobileLike("%" + mobile + "%");
			examplePage.getOredCriteria().add(criteria);
		}
		if (username != null && !username.equals("")) {
			agentid = usersService.selectByUsername(username).getId();
			request.setAttribute("agentid", agentid);
			criteria = criteria.andAgentidLike("%" + agentid + "%");
			examplePage.getOredCriteria().add(criteria);
		}
		switch (adminUsergrade) {
		case 0:

			break;
		default:
			examplePage.getOredCriteria().add(criteria.andAgentidEqualTo(adminUserId));
			break;
		}/*
			 * 设置分页
			 */
		int totlenum = registerinfoesService.countByExample(examplePage);// 页面总数
		request.setAttribute("totlenum", totlenum);// 总数据传入session中
		request.setAttribute("curPage", curPage);// 当前页面传入session中
		smsinfoes = registerinfoesService.selectByExamplePage(examplePage);
		if (smsinfoes != null) {
			for (Smsinfoes s : smsinfoes) {
				if(s.getAgentid() != null) {
				   username = usersService.selectByPrimaryKey(s.getAgentid()).getUsername();
				   s.setAgentid(username);
				   agentid=username;
				}else {
					s.setApistatus("请求失败");
				}
			}
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_yanzhengmabaobiao");
			for (Smsinfoes s : smsinfoes) {
				if (s.getApistatus() != null) {
					ApiState newstate = GsonUtil.getObject(s.getApistatus(), ApiState.class);
					s.setApistatus(newstate.getDesc());
				} else {
					s.setApistatus("请求失败");
				}
			}
			request.setAttribute("list", smsinfoes);
			request.setAttribute("agentid", agentid);
			return modelAndView;
		}
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}
}
